﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XrmLibrary.MsCrm4Adapter.Extensions
{
    public static class TimeInfoExtensions
    {
        public static Microsoft.Crm.Sdk.Messages.TimeInfo ToCrm2011TimeInfo(this Microsoft.Crm.Sdk.TimeInfo crm4TimeInfo)
        {
            Microsoft.Crm.Sdk.Messages.TimeInfo crm2011TimeInfo =
                new Microsoft.Crm.Sdk.Messages.TimeInfo
                {
                    ActivityStatusCode = crm4TimeInfo.ActivityStatusCode,
                    CalendarId = crm4TimeInfo.CalendarId,
                    DisplayText = crm4TimeInfo.DisplayText,
                    Effort = crm4TimeInfo.Effort,
                    End = crm4TimeInfo.End.ToDateTime(),
                    IsActivity = crm4TimeInfo.IsActivity,
                    SourceId = crm4TimeInfo.SourceId,
                    SourceTypeCode = crm4TimeInfo.SourceTypeCode,
                    Start = crm4TimeInfo.Start.ToDateTime(),
                    SubCode = crm4TimeInfo.SubCode.ToCrm2011SubCode(),
                    TimeCode = crm4TimeInfo.TimeCode.ToCrm2011TimeCode()
                };

            return crm2011TimeInfo;
        }

        public static IEnumerable<Microsoft.Crm.Sdk.Messages.TimeInfo> ToCrm2011TimeInfoCollection(this Microsoft.Crm.Sdk.TimeInfo[] crm4TimeInfo)
        {
            foreach (Microsoft.Crm.Sdk.TimeInfo timeInfo in crm4TimeInfo)
            {
                yield return timeInfo.ToCrm2011TimeInfo();
            }
        }

        public static IEnumerable<Microsoft.Crm.Sdk.Messages.TimeInfo[]> ToCrm2011TimeInfoCollection(this Microsoft.Crm.Sdk.TimeInfo[][] crm4TimeInfo)
        {
            foreach (Microsoft.Crm.Sdk.TimeInfo[] timeInfo in crm4TimeInfo)
            {
                yield return timeInfo.ToCrm2011TimeInfoCollection().ToArray();
            }
        }
    }
}
